package com.example.demo.suanfa_jiegou;

import java.lang.module.FindException;

/**
 * @program: java_base
 * @description: 用数组形式 实现queue 和stack
 * @author: zhouhongtao
 * @happyCreateTime: 2022/02/10 16:40
 */
public class RingArray {

    /**
     * queue : 队列 先入先出 后入后出
     */
    // 因为数组长度限定，顾需要规定长度
    public static class MyQueue {
        private int[] arr;

        private int pushi;
        private int polli;
        // 此时数组大小
        private int size;
        // capacity
        private int limit;

        public MyQueue(int capacity) {
            arr = new int[capacity];
            pushi = polli = size = 0;
            this.limit = capacity;
        }

        public void push(int value) {
            if (size > arr.length) {
                // throw new Exception()
                return;
            }
            if (size == limit){
                System.out.println("超长了");
                return;
            }
            arr[pushi++] = value;
            size++;
        }

//        private void add(int[] arr, int index, int value) {
//            int i = (index > arr.length - 1) ? 0 : index;
//            arr[i] = value;
//        }

        public int pop() {
            if (size == 0) {
                return -1;
            }
            size--;
            return arr[polli++];
        }

        private int nextIndex(int index){
            return index > limit - 1 ? 0 : ++index;
        }
    }


    public static void main(String[] args) {
        MyQueue queue = new MyQueue(10);
        queue.push(1);
        queue.push(2);
        queue.push(3);
        queue.push(4);
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
    }


    public static  class MyStack{
        int[] arr;
        int index;
        int capacity;

        public MyStack(int size){
            arr = new int[size];
            index = 0;
            capacity = size;
        }

        public void push(int value){
            if (index == capacity){
                return;
            }
            arr[index++] = value;
        }

        public int pop(){
            if (index == 0){
                return -999;
            }
            return arr[--index];
        }

    }


//    public static void main(String[] args) {
//        MyStack stack = new MyStack(5);
//        stack.push(1);
//        stack.push(4);
//        stack.push(5);
//        stack.push(6);
//        stack.push(7);
//        stack.push(7);
//        stack.push(7);
//        stack.push(7);
//        stack.push(7);
//        System.out.println(stack.pop());
//        System.out.println(stack.pop());
//        System.out.println(stack.pop());
//        System.out.println(stack.pop());
//        System.out.println(stack.pop());
//        System.out.println(stack.pop());
//
//
//    }

    static class MyQueue1{

        int inPoint;
        int outPoint;
        int size;
        int capacity;
        int[] arr;

        public MyQueue1( int capacity) {
            this.capacity = capacity;
            inPoint= 0;
            outPoint = 0;
            size = 0;
            arr = new int[capacity];
        }

        public void add(int value){
            if (size < capacity){
                arr[inPoint] = value;
                inPoint = getPoint(inPoint);
                size++;
            }
        }

        public int pop(){
            if (size != 0){
                size--;
                int ans = arr[outPoint];
                outPoint = getPoint(outPoint);
                return ans;
            }else {
                return -1;
            }
        }


        private int getPoint(int point){
            return point < capacity - 1 ? point + 1 : 0;
        }
    }


}
